package com.efast.dmom.mgmt.service.mes.qm.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.util.date.DateUtils;
import com.efast.dmom.mgmt.bean.mes.qm.QMCheckAreaBean;
import com.efast.dmom.mgmt.bean.mes.qm.QMCheckDefectCodeBean;
import com.efast.dmom.mgmt.bean.mes.qm.QMCheckDefectSaveBean;
import com.efast.dmom.mgmt.bean.mes.qm.QMCheckItemBean;
import com.efast.dmom.mgmt.bean.mes.qm.QMCheckPartBean;
import com.efast.dmom.mgmt.bean.mes.qm.QMRepairRecordSaveBean;
import com.efast.dmom.mgmt.bean.mes.qm.RepairConsumptionSaveBean;
import com.efast.dmom.mgmt.dao.mes.TwBasLotConsumpationDao;
import com.efast.dmom.mgmt.dao.mes.TwBasLotDao;
import com.efast.dmom.mgmt.dao.mes.qm.QMDefectDao;
import com.efast.dmom.mgmt.dao.mes.qm.QMDefectRepairDao;
import com.efast.dmom.mgmt.entity.mes.TwBasLot;
import com.efast.dmom.mgmt.entity.mes.TwBasLotConsumpation;
import com.efast.dmom.mgmt.service.master.TmBasPartSnService;
import com.efast.dmom.mgmt.service.mes.qm.IQMDefectService;
import com.efast.dmom.mgmt.utils.Assist;

@Service
public class QMDefectService implements IQMDefectService {

	@Autowired
	QMDefectDao defectDao;
	
	@Autowired
	QMDefectRepairDao repairDao;
	
	@Autowired
	TwBasLotDao lotDao;
	
	@Autowired
    private TmBasPartSnService tmBasPartSnService;
	
	@Autowired
	private TwBasLotDao twBasLotDao;
	
	@Autowired 
	private TwBasLotConsumpationDao twBasLotConsumpationDao;

	@Override
	public List<QMCheckAreaBean> getCheckAreaByStationId(Integer stationId) {

		return defectDao.getCheckAreaByStationId(stationId);
	}

	@Override
	public List<QMCheckPartBean> getCheckPartsByAreaId(Integer areaId) {

		return defectDao.getCheckPartsByAreaId(areaId);
	}

	@Override
	public List<QMCheckItemBean> getCheckItemsByPartId(Integer partId) {

		return defectDao.getCheckItemsByPartId(partId);
	}

	@Override
	public List<QMCheckDefectCodeBean> getDefectCodeByItemId(Integer itemId) {

		return defectDao.getDefectCodeByItemId(itemId);
	}

	@Override
	public void saveDefectRecord(List<QMCheckDefectSaveBean> list) {
		
		defectDao.saveDefectRecord(list);
	}

	@Override
	public void deleteDefectByDefectId(Integer id) {
		
		QMCheckDefectSaveBean saveBean = new QMCheckDefectSaveBean();
		
		saveBean.setLastUpdateTime(new Date());
		saveBean.setLastUpdateUser(SpringWebUtils.getRequestUser());
		saveBean.setStatus("已关闭");
		// saveBean.setRepairStatus("已维修");
		saveBean.setId(id);
		
		defectDao.deleteDefect(saveBean);
	}
	
	@Override
	public void RepairRecordStatus(Integer id) {
QMCheckDefectSaveBean saveBean = new QMCheckDefectSaveBean();
		
		saveBean.setLastUpdateTime(new Date());
		saveBean.setLastUpdateUser(SpringWebUtils.getRequestUser());
		// saveBean.setStatus("已关闭");
		 saveBean.setRepairStatus("已维修");
		saveBean.setId(id);
		defectDao.RepairRecordStatus(saveBean);
		
	}
	 @Override
	public ResultBean saveRepairRecord(QMRepairRecordSaveBean saveBean) {
		
		ResultBean resultBean = new ResultBean(true, null);
		
		List<QMRepairRecordSaveBean> saveBeanList = new ArrayList<QMRepairRecordSaveBean>();
		List<RepairConsumptionSaveBean> defectRepairDate = new ArrayList<RepairConsumptionSaveBean>();
		List<RepairConsumptionSaveBean> list = saveBean.getKeyPartsReplace();
		List<TwBasLotConsumpation> consumpationList = new ArrayList<TwBasLotConsumpation>();
		saveBean.setLotName(saveBean.getLotName());
		
		if(list != null && list.size() > 0){
			
			for(int i=0; i<list.size(); i++){
				
				RepairConsumptionSaveBean bean = list.get(i);
				
				String sn = bean.getNewConsumedPartSn();
				if (saveBean.getMessage().equals("关键件") && sn != null && sn != "" && bean.getReplaceNumber() != null && bean.getReplaceNumber() != 0) {
					defectRepairDate.add(bean);
					// 当是在物料消耗表中查询到的记录时
					if (bean.getOldConsumedPartSn() != null && bean.getOldConsumedPartSn() != "" && !bean.getOldConsumedPartSn().equals(sn)) {
						Assist assist = new Assist();
						 assist.setRequires(Assist.andEq("consumption_id",bean.getId()));
						List<TwBasLotConsumpation> selectTwBasLotConsumpation = twBasLotConsumpationDao.selectTwBasLotConsumpation(assist);
						if (selectTwBasLotConsumpation.size() == 1 && sn != null && !"".equals(sn)) {
							TwBasLotConsumpation twBasLotConsumpation = selectTwBasLotConsumpation.get(0);
							twBasLotConsumpation.setConsumedPartSn(bean.getNewConsumedPartSn());
							twBasLotConsumpation.setCreateTime(new Date());
							twBasLotConsumpation.setLastUpdateTime(DateUtils.getCurrentDateAsString("yyyy-MM-dd HH:mm:ss"));
							twBasLotConsumpation.setCreateUser(SpringWebUtils.getRequestUser());
							twBasLotConsumpation.setLastUpdateUser(SpringWebUtils.getRequestUser());
							twBasLotConsumpation.setReplaceNumber(bean.getReplaceNumber());
							if (bean.getComments() != null && bean.getComments() != "") {
								twBasLotConsumpation.setComments(bean.getComments()+"TH");
								twBasLotConsumpation.setReplaceConsumedPartSn(bean.getOldConsumedPartSn());
							} else {
								twBasLotConsumpation.setComments("TH");
								twBasLotConsumpation.setReplaceConsumedPartSn(bean.getOldConsumedPartSn());
							}
							consumpationList.add(twBasLotConsumpation);
							// repairDao.addConsumpation(twBasLotConsumpation);
						}
						QMRepairRecordSaveBean tmp = new QMRepairRecordSaveBean();
						tmp.setReplaceNumber(bean.getReplaceNumber());
						tmp.setRepairCode(saveBean.getRepairCode());
						tmp.setRepairGroup(saveBean.getRepairGroup());
						tmp.setComments(saveBean.getComments());
						tmp.setVin(bean.getVin());
						tmp.setStation(saveBean.getStation());
						tmp.setConsumptionId(bean.getId());
						tmp.setBomItemName(bean.getBomItemName());
						tmp.setConsumedPartNoVersion(bean.getConsumedPartNoVersion());
						tmp.setIsKeyPartReplace(saveBean.getIsKeyPartReplace());
						tmp.setOldConsumedPartSn(bean.getOldConsumedPartSn());
						tmp.setNewConsumedPartSn(bean.getNewConsumedPartSn());
						tmp.setRepairer(SpringWebUtils.getRequestUser());
						tmp.setCreateTime(new Date());
						tmp.setCreateUser(SpringWebUtils.getRequestUser());
						tmp.setLastUpdateTime(new Date());
						tmp.setLastUpdateUser(SpringWebUtils.getRequestUser());
						tmp.setRepairer(SpringWebUtils.getRequestUser());
						if (bean.getComments() != null && bean.getComments() != "") {
							tmp.setComments(bean.getComments()+"TH");
						} else {
							tmp.setComments("TH");
						}
						
						saveBeanList.add(tmp);
					}
					
				}
			}
			if(saveBeanList.size() > 0){
				repairDao.updateConsumpationReplace(saveBeanList);
			}
		}
			
		saveBeanList.clear();
		saveBean.setRepairer(SpringWebUtils.getRequestUser());
		saveBean.setCreateTime(new Date());
		saveBean.setCreateUser(SpringWebUtils.getRequestUser());
		List<JSONObject> newList = new ArrayList<JSONObject>();
		if (saveBean.getMessage().equals("标准件")) {
			for (RepairConsumptionSaveBean RepairConsumptionSaveBean : list) {
				if (RepairConsumptionSaveBean.getReplaceNumber() != null && RepairConsumptionSaveBean.getReplaceNumber() != 0) {
					saveBean.setReplacePartNoVersion(list.get(0).getReplacePartNoVersion());
					saveBean.setReplaceNumber(list.get(0).getReplaceNumber());
					saveBean.setReplaceNumber(RepairConsumptionSaveBean.getReplaceNumber());
					saveBean.setIsKeyPartReplace("flase");
					newList.add((JSONObject) JSONObject.toJSON(saveBean));
				}
			}
		} else if (saveBean.getMessage().equals("关键件")) {
			for (RepairConsumptionSaveBean repairConsumptionSaveBean : defectRepairDate) {
				saveBean.setOldConsumedPartSn(repairConsumptionSaveBean.getOldConsumedPartSn());
				saveBean.setNewConsumedPartSn(repairConsumptionSaveBean.getNewConsumedPartSn());
				saveBean.setReplaceNumber(repairConsumptionSaveBean.getReplaceNumber());
				newList.add((JSONObject) JSONObject.toJSON(saveBean));
			}
		}else if (saveBean.getMessage() == null || saveBean.getMessage() == "") {
			newList.add((JSONObject) JSONObject.toJSON(saveBean));
		}
		for (JSONObject jsonObject : newList) {
			saveBeanList.add(JSONObject.toJavaObject(jsonObject, QMRepairRecordSaveBean.class));
		}
		defectDao.saveRepairRecord(saveBeanList);
		
		return resultBean;
	}

	@Override
	public boolean showDefectSection(Integer stationId) {
		
		Integer count = defectDao.showDefectSection(stationId);
		
		return count > 0 ? true : false;
	}

	@Override
	public ResultBean selectRowNumByLot(Map<String,Object> map) {
		ResultBean resultBean = new ResultBean();
		String lotName = (String)map.get("lotName");
		TwBasLot lotData = twBasLotDao.selectTwBasLotByname(lotName);
		Integer selectRowNumByLot = null;
		if (lotData.getLotType().equals("A")) {
			if ((lotData.getSelfNumber() != null && lotData.getSelfNumber() !="") && (lotData.getVin() == null || lotData.getVin() =="")) {
				selectRowNumByLot = defectDao.selectRowNumByLot(lotName);
				if (selectRowNumByLot == 0 ||selectRowNumByLot == null ) {
					resultBean.setSuccess(true);
					resultBean.setMessage("此工位为质量门,所有缺陷已关闭！");
				} else {
					if (map.get("btn").toString().equals("startBtn") && selectRowNumByLot >0) {
						defectDao.updateDefectRecord(map);
					}
					resultBean.setSuccess(false);
					resultBean.setMessage("此工位为质量门，有"+selectRowNumByLot+"个缺陷未关闭！");
					resultBean.setObject(selectRowNumByLot);
				}
			} else if ((lotData.getSelfNumber() == null || lotData.getSelfNumber() =="") && (lotData.getVin() != null && lotData.getVin() !="")) {
				selectRowNumByLot = defectDao.selectRowNumByLot(lotName);
				if (selectRowNumByLot == 0 ||selectRowNumByLot == null) {
					resultBean.setSuccess(true);
					resultBean.setMessage("此工位为质量门,所有缺陷已关闭！");
				} else {
					if (map.get("btn").toString().equals("startBtn") && selectRowNumByLot >0) {
						defectDao.updateDefectRecord(map);
					}
					resultBean.setSuccess(false);
					resultBean.setMessage("此工位为质量门，有"+selectRowNumByLot+"个缺陷未关闭！");
					resultBean.setObject(selectRowNumByLot);
				}
			} else if (lotData.getSelfNumber() != null && lotData.getSelfNumber() !="" && lotData.getVin() != null && lotData.getVin() !="") {
				selectRowNumByLot = defectDao.selectRowNumByLot(lotName);
				if (map.get("btn").toString().equals("startBtn") && selectRowNumByLot >0) {
					defectDao.updateDefectRecord(map);
				}
				TwBasLot selectSelfnumber = twBasLotDao.selectlotBySelfNumberLotname(lotData.getSelfNumber(), lotName);
				Integer SelfNumberSum = defectDao.selectRowNumByLot(selectSelfnumber.getLotName());
				if (map.get("btn").toString().equals("startBtn") && SelfNumberSum >0) {
					map.replace("lotName", selectSelfnumber.getLotName());
					defectDao.updateDefectRecord(map);
				}
				if ((selectRowNumByLot == null || selectRowNumByLot == 0) && (SelfNumberSum == 0 ||SelfNumberSum == null)) {
					resultBean.setSuccess(true);
					resultBean.setMessage("此工位为质量门,所有缺陷已关闭！");
				} else  {
					Integer sum = (selectRowNumByLot == null?0:selectRowNumByLot)+(SelfNumberSum == null?0:SelfNumberSum);
					resultBean.setSuccess(false);
					resultBean.setMessage("此工位为质量门，有"+sum+"个缺陷未关闭！");
					resultBean.setObject(sum);
				}
			}
		} else {
			selectRowNumByLot = defectDao.selectRowNumByLot(lotName);
			// 当有缺陷存在时更新质量门信息
			if (map.get("btn").toString().equals("startBtn") && selectRowNumByLot >0) {
				defectDao.updateDefectRecord(map);
			}
			if (selectRowNumByLot == 0 || selectRowNumByLot == null) {
				resultBean.setSuccess(true);
				resultBean.setMessage("此工位为质量门,所有缺陷已关闭！");
			} else {
				resultBean.setSuccess(false);
				resultBean.setMessage("此工位为质量门，有"+selectRowNumByLot+"个缺陷未关闭！");
				resultBean.setObject(selectRowNumByLot);
			}
		}
		return resultBean;
	}

}
